package com.rui.common.util;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * 响应类
 *
 * @author xr
 * @email xr@qq.com
 * @date 2020-01-01 00:00:00
 */
@Data
public class Res<T> {
    private static final long serialVersionUID = 1L;
    @ApiModelProperty("响应码")
    private int code;
    @ApiModelProperty("消息")
    private String msg;
    @ApiModelProperty("数据")
    private T data;

    public Res() {
        super();
    }

    public Res(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public Res(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public Res success() {
        return success(HttpStatus.SUCCESS.getMsg());
    }

    public Res success(String msg) {
        return new Res(HttpStatus.SUCCESS.getCode(), msg);
    }

    public Res failure() {
        return failure(HttpStatus.FAILURE);
    }

    public Res failure(String msg) {
        return new Res(HttpStatus.FAILURE.getCode(), msg);
    }

    public Res failure(Integer code, String msg) {
        return new Res(code, msg);
    }

    public Res failure(HttpStatus httpStatus) {
        return new Res(httpStatus.getCode(), httpStatus.getMsg());
    }

    public Res data(T data) {
        return new Res(HttpStatus.SUCCESS.getCode(), HttpStatus.SUCCESS.getMsg(), data);
    }

    public Res data(T data, String msg) {
        return new Res(HttpStatus.SUCCESS.getCode(), msg != null ? msg : HttpStatus.SUCCESS.getMsg(), data);
    }

}
